package utils

import (
	"sync"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var db *gorm.DB
var one sync.Once

// InitDB initializes and returns a GORM DB instance

func GetDB() (*gorm.DB, error) {
	var err error
	one.Do(func() {
		db, err = initDB()
	})
	return db, err
}

func initDB() (*gorm.DB, error) {
	dsn := "root:root@tcp(172.24.73.24:3307)/hc_zhm?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		return nil, err
	}
	return db, nil
}
